package DBLayer;
import java.sql.*;
import java.util.Properties;
import com.mysql.jdbc.Driver;

public class SQLConnector {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    //MySQL Connection Settings.
//    private static String server   = "remote-mysql4.servage.net";
    private static String server   = "localhost";
    private static String database = "PrestaCopy";
    private static String username = "PrestaCopy";
    private static String password = "tRagU7Af";

    private DatabaseMetaData dma;
    private static Connection conn;
    private static SQLConnector instance = null;
    // </editor-fold>

    //Constructor.
    private SQLConnector() {
        registerDriver();
        openConnection();
        getMetaData();
    }

    //registerDriver(): registering Driver.
    private void registerDriver() {
	//Registering Driver.
        try {
            DriverManager.registerDriver (new Driver());
            System.out.println("Driver registered succesfully.");
	} catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }

    //getMetaData(): Getting Meta Data.
    private void getMetaData() {
        try {
            dma = conn.getMetaData();
            System.out.println("Driver:                " + dma.getDriverName());
            System.out.println("Connection String:     " + dma.getURL());
            System.out.println("Database Product Name: " + dma.getDatabaseProductName());
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }

    //openConnection(): Opens the MySQL connection.
    private void openConnection() {
        
        //Building Connection String.
        String url = "jdbc:mysql://" + server + "/" + database + "?user=" + username + "&password=" + password;

        //Opening SQL Connections.
        try {
            Properties connProperties = new Properties();
            connProperties.put("autoReconnect", "true");
            DriverManager.registerDriver (new Driver());
            conn = DriverManager.getConnection(url, connProperties);
            conn.setAutoCommit(true);
            System.out.println("SQL Connection established succesfully.");
        } catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("VendorError: " + e.getErrorCode());
        }
    }

    //closeConnection(): Closes the MySQL connection.
    public void closeConnection() {
        try {
            conn.close();
            System.out.println("SQL Connection closed succesfully.");
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
        }
    }

    //getConnection(): Returns the MySQL connection.
    public Connection getConnection() {
        return conn;
    }

    //getInstance(): Returns the instance of the MySQL connection.
    public static SQLConnector getInstance() {
        if (instance == null) {
            instance = new SQLConnector();
        }
        return instance;
    }
}
